package com.alan.mvvm;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.alan.base.ui.BaseActivity;
import com.alan.mvvm.base.BaseViewModel;
import com.alan.mvvm.base.IBaseComponents;
import com.alan.mvvm.utils.Utils;


public class BaseVMActivity<VB extends ViewDataBinding, VM extends BaseViewModel> extends BaseActivity implements IBaseComponents<VB, VM> {

    protected VB mVB;
    protected VM mVM;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initViewModel();
        initViewBinding();
    }

    protected void initViewModel(){
        mVM = Utils.initViewModel(this, this);
        getLifecycle().addObserver(mVM);
    }

    protected void initViewBinding(){
        mVB = DataBindingUtil.setContentView(this, Utils.getRes(this, this));
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        mVM.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getLifecycle().removeObserver(mVM);
    }
}
